package com.haixinga.commbiz.member.ctrl.oss;

import com.alibaba.fastjson.JSONObject;
import com.haixinga.commbiz.commons.utils.AliyunOssKit;
import com.haixinga.commbiz.commons.utils.FileKit;
import com.haixinga.commbiz.commons.vo.ApiRes;
import com.haixinga.commbiz.member.ctrl.CommonCtrl;
import com.haixinga.commbiz.member.utils.MemberPropKit;
import com.haixinga.commbiz.commons.exception.BizException;
import com.haixinga.commbiz.commons.utils.StringKit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value = "api/oss")
public class AliyunOssController extends CommonCtrl {

    private static final Logger logger = LoggerFactory.getLogger(AliyunOssController.class);

    /** 签名 **/
    @RequestMapping(value = "/formParams", method = RequestMethod.POST)
    public ApiRes genFormParams(){

        String sourceFileName = getValStringRequired("sourceFileName");  //用户上传的原始文件名称
        String bizTypeCode = getValStringRequired("bizTypeCode");  //业务编码

        //文件名称 =  {uuid}.jpg
        String fileSuffix = FileKit.getFileSuffix(sourceFileName, true);
        if(fileSuffix == null) throw new BizException("获取文件类型失败！");

        String uploadFileName = StringKit.getUUID() + fileSuffix;

        //获取到上传的最终路径
        String fileNameAndDir = "dir";

        //生成oss请求数据
        JSONObject result = AliyunOssKit.genFormParams(fileNameAndDir, MemberPropKit.getOssConfig());

        return ApiRes.ok(result);
    }
}
